import ImageMonitor from './ImageMonitor'
import pigGoldImg from '@/assets/images/pig-gold.png'
import pigRedImg from '@/assets/images/pig-red.png'
/**
 * 素材构造函数
 */
export default function Material(_this, materialType, marginLeft, materialId) {
  this.speedUpTime = 300
  this.materialId = materialId
  this.materialType = materialType
  this.materialWidth = 50
  this.materialHeight = 50
  this.materialMarginLeft = marginLeft
  this.materialMarginTop = -50
  this.speed = 0.04 * Math.pow(1.2, Math.floor(_this.time / this.speedUpTime))
  this.loop = 0
  let materialItemSrc = ''
  // 根据类型赋值图片
  switch (this.materialType) {
    case 0:
      materialItemSrc = pigGoldImg
      break
    case 1:
      materialItemSrc = pigRedImg
      break
  }
  const imageMonitor = new ImageMonitor()
  this.materialItem = imageMonitor.createImage(materialItemSrc)
  // 绘制
  this.paint = (ctx) => {
    ctx.drawImage(this.materialItem, this.materialMarginLeft, this.materialMarginTop, this.materialWidth, this.materialHeight)
  }
  // 移动
  this.move = (ctx) => {
    if (_this.time % this.speedUpTime === 0) {
      this.speed *= 1.4
    }
    this.materialMarginTop += ++this.loop * this.speed
    if (this.materialMarginTop > _this.clientHeight) {
      _this.materialList[this.materialId] = null
    } else {
      this.paint(ctx)
    }
  }
}